// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-11-05 10:56:25
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 15:11:16
 * @FilePath: /SGSim/Import/BDFImport/UnitTest/PARAMETER/PARAMETERTest.cpp
 */

#include "gtest/gtest.h"

#include "Utility/FileSystem/SGFileSystem.h"
#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Parameter/INLPARMService.h"
#include "DBManager/IService/Input/Parameter/IParameterService.h"

#include "Import/BDFImport/BDFImport.h"
#include "Import/BDFImport/BDFParserBase.h"
#include "Import/BDFImport/BulkData/Parameter/NLPARMParser.h"

using namespace SG::DataStructure::FEM;
using namespace SG::DBManager;
using namespace SG::Import;

extern std::shared_ptr<DBServiceFactory> DB_SERVICEFACTORY;
extern std::vector<std::string>          dividedEntryInfo;

class PARAMETERTest : public ::testing::Test
{
  public:
    static void SetUpTestSuite ()
    {
        DB_SERVICEFACTORY->ClearDatabase ();
    }
};

TEST_F (PARAMETERTest, NLPARMTest)
{
    // 1 假设BDF文件文本内容，通过解析函数加入到数据库中（需要更改）
    std::vector<std::string> Entry = { "NLPARM   1       10              AUTO    5       25              NO" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseNLPARM (DB_SERVICEFACTORY, dividedEntryInfo);

    SG::DataStructure::FEM::NLPARMData tempData;
    DB_SERVICEFACTORY->get<INLPARMService> ()->FindByIndex (0, tempData);

    SG::DataStructure::FEM::NLPARMData tempData2;
    tempData2.m_id       = 1;
    tempData2.m_NINC     = 10;
    tempData2.m_DT       = 0.0;
    tempData2.m_KMETHOD  = 1;
    tempData2.m_KSTEP    = 5;
    tempData2.m_MAXITER  = 25;
    tempData2.m_CONV     = -1;
    tempData2.m_INTOUT   = 0;
    tempData2.m_EPSU     = 0.001;
    tempData2.m_EPSP     = 0.001;
    tempData2.m_EPSW     = 1.0E-7;
    tempData2.m_MAXDIV   = 3;
    tempData2.m_MAXQN    = 25;
    tempData2.m_MAXLS    = 4;
    tempData2.m_FSTRESS  = 0.2;
    tempData2.m_LSTOL    = 0.5;
    tempData2.m_MAXBIS   = 5;
    tempData2.m_MAXR     = 20.0;
    tempData2.m_RTOLB    = 20.0;
    tempData2.m_MINITER  = 0;
    tempData2.m_domainId = 1;
    ASSERT_TRUE (tempData2 == tempData);
}

TEST_F (PARAMETERTest, ExecutiveSystemParam)
{
    DB_SERVICEFACTORY->ClearDatabase ();

    auto _imp     = std::make_shared<BDFImporter> (DB_SERVICEFACTORY);
    auto filePath = SG::Utility::FileSystem::getTestDataPath ("Import/BDF/ExecutiveSystemParam.bdf");
    _imp->Load (filePath.c_str ());

    auto result = DB_SERVICEFACTORY->get<IParameterService> ()->GetExecutiveSystemParameter ();
    SGInfo << result.PrettyPrintJsonString ();

    ASSERT_EQ (true, result.IsArray ());

    ASSERT_EQ (3, result.size ());

    for (size_t i = 0; i < result.size (); i++)
    {
        SG::Utility::SGParameter param = result[i];
        if (param["KEY"].GetString () == "SYSTEM")
        {
            ASSERT_EQ ("316", param["I"].GetString ());
            ASSERT_EQ ("19", param["VALUE"].GetString ());
        }
        else if (param["KEY"].GetString () == "STPFLG")
        {
            ASSERT_EQ ("1", param["VALUE"].GetString ());
            ASSERT_EQ (1, std::stoi (param["VALUE"].GetString ()));
        }
    }
}
